﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using System.Data;
using CdcSoftware.Pivotal.Applications.Core.Common;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using Pcm.Retail.Client;
using System.Windows.Forms;
using CdcSoftware.Pivotal.Engine.UI.Forms;
using CdcSoftware.Pivotal.Engine.UI.DataTypes.Navigation;
using CdcSoftware.Pivotal.Engine.Client.Types;
using CdcSoftware.Iaf.SmartClient.Message;

namespace Pcm.Retail.Client.FormTasks
{
    public partial class RE_Promotion_SelectionList : FormClientTask
    {   
        /// <summary>
        /// This method gets called when the form meta data and data have both been loaded,
        /// and the form UI has finished drawing. Override this method to perform custom
        /// form initialization.
        /// </summary>
        /// <history>
        /// #Revision   Date        Author  Description
        /// 6.0.12.1    08-03-2013  DS      Initial Version
        /// </history>
        public override void OnFormInitialized()
        {
            try
            {
                base.OnFormInitialized();

                //Remove the taskpads.
                if (this.TaskPad != null)
                    this.TaskPad.Visible = false;

                AddRemoveButtons();

                this.PrimaryDataRow["Product_Id"] = TypeConvert.ToDBValue(Id.Create(this.TransitionPointParameter.GetUserDefinedParameter(3)));
                this.PrimaryDataRow["Customer_Type"] = this.TransitionPointParameter.GetUserDefinedParameter(4);
                this.PrimaryDataRow["Store_Id"] = TypeConvert.ToDBValue(Id.Create(this.TransitionPointParameter.GetUserDefinedParameter(5)));
                this.PrimaryDataRow["Inventory_Store_Id"] = TypeConvert.ToDBValue(Id.Create(this.TransitionPointParameter.GetUserDefinedParameter(6)));

                PivotalSearchResultsGrid promotionSelectionList = this.FormControl.GetControlByName("promotionSelectionList") as PivotalSearchResultsGrid;
                promotionSelectionList.ResultsViewControl.EnableContextMenu = false;
                
                if (string.Equals(TypeConvert.ToString(this.TransitionPointParameter.GetUserDefinedParameter(1)), "Tier 1"))
                {
                    promotionSelectionList.ResultsViewControl.AllowMultiRowSelection = false;
                    //Check for Mandatory and apply the filter.
                    if (TypeConvert.ToBoolean(this.TransitionPointParameter.GetUserDefinedParameter(2)))
                    {
                        promotionSelectionList.FilterProperties.FilterId = Globals.SystemClient.UserProfile.GetMetaItem<CdcSoftware.Pivotal.Engine.Types.Searches.SystemSearch>
                            ("RE_Mandatory Promo for Prod? CustType? Store?").Id.ToString();
                    }
                    else
                        promotionSelectionList.FilterProperties.FilterId = Globals.SystemClient.UserProfile.GetMetaItem<CdcSoftware.Pivotal.Engine.Types.Searches.SystemSearch>
                            ("RE_Tier1 Promo for Prod? CustType? Store?").Id.ToString();                   
                }
                else
                {
                    promotionSelectionList.ResultsViewControl.AllowMultiRowSelection = true;
                    //Apply the secondary filter.
                    promotionSelectionList.FilterProperties.FilterId = Globals.SystemClient.UserProfile.GetMetaItem<CdcSoftware.Pivotal.Engine.Types.Searches.SystemSearch>
                            ("RE_Tier2 Promo for Prod? CustType? Store?").Id.ToString();
                }
                promotionSelectionList.FilterProperties.FilterParameters = "Product_Id,Customer_Type,Store_Id,Inventory_Store_Id";
                this.FormView.Dirty = false;
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc);
            }
        }

        /// <summary>
        /// Mouse double click on the grid.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void ResultsViewControl_CellMouseDoubleClick(object sender, EventArgs e)
        {
            Pcm.Retail.Client.FormTasks.CommandHandlers.RE_Promotion_SelectionListCommandHandlers command = new CommandHandlers.RE_Promotion_SelectionListCommandHandlers();
            command.OnClickSelectPromotion();
        }

        //DS: 22082013 - Code change for Enter key hit when there is no row selected. The error was getting logged due to this. 
        //Also allowed Enter key to work as selestion of the Promotions.
        /// <summary>
        /// On Enter key press, fire the selection of the promotion and cancel the default event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void ResultsViewControl_EnterKeyPressed(object sender, EventArgs e)
        {
            System.ComponentModel.CancelEventArgs args = e as System.ComponentModel.CancelEventArgs;
            Pcm.Retail.Client.FormTasks.CommandHandlers.RE_Promotion_SelectionListCommandHandlers command = new CommandHandlers.RE_Promotion_SelectionListCommandHandlers();
            command.OnClickSelectPromotion();
            args.Cancel = true;
        }
    }
}
